package com.qc.serviceedu.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qc.commonutils.result.R;
import com.qc.commonutils.result.ResultCode;
import com.qc.serviceedu.entity.EduCourse;
import com.qc.serviceedu.entity.EduTeacher;
import com.qc.serviceedu.entity.chapter.ChapterVo;
import com.qc.serviceedu.entity.vo.CourseInfoVo;
import com.qc.serviceedu.entity.vo.CoursePublicVo;
import com.qc.serviceedu.query.CourseQuery;
import com.qc.serviceedu.query.TeacherQuery;
import com.qc.serviceedu.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author AltmanQc
 * @since 2020-12-25
 */
@Api("课程管理")
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    //课程显示带分页 TODO
    //完善条件查询带分类
    @ApiOperation(value = "讲师列表分页查询")
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public R pageCourseCondition(@PathVariable long current, @PathVariable long limit,
                                 @RequestBody(required = false) CourseQuery courseQuery){  //@RequestBody(required = false)参数值可以为空
        Page<EduCourse> pageParam = new Page<>(current, limit);
        courseService.pageQuery(pageParam,courseQuery);
        List<EduCourse> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }


    @ApiOperation("添加课程")
    @PostMapping("addCourse")
    public R addCourse(@RequestBody CourseInfoVo courseInfoVo){
        String id = courseService.saveCourse(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    //根据课程id查询课程的基本信息
    @ApiOperation("根据课程id查询课程的基本信息")
    @GetMapping("getCourse/{courseId}")
    public R getCourse(@PathVariable String courseId){
        CourseInfoVo courseInfo = courseService.getCourseInfo(courseId);
        return R.ok().data("items",courseInfo);
    }

    //修改课程信息
    @ApiOperation("修改课程信息")
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfoVo(courseInfoVo);
        return R.ok();
    }

    //根据课程id查询课程确认信息
    @ApiOperation("根据课程id查询课程确认信息")
    @GetMapping("getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(@PathVariable String courseId){
        CoursePublicVo courseInfo = courseService.publishCourseInfo(courseId);
        return R.ok().data("items",courseInfo);
    }

    //课程的最终发布
    //修改课程的发布状态
    @PostMapping("publishCourse/{courseId}")
    public R publishCourse(@PathVariable String courseId){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus(ResultCode.COURSE_NORMAL);
        courseService.updateById(eduCourse);
        return R.ok();
    }

    //删除课程信息
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        courseService.deleteCourse(courseId);
        return R.ok();
    }

}

